<?php

namespace App\Admin\Imports;

use App\Models\AdminUserModel;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\SkipsFailures;
use Illuminate\Support\Facades\Hash;




class Merchant implements ToModel ,WithStartRow ,WithValidation, SkipsOnFailure
{
    use Importable, SkipsFailures;

    public function model(array $row)
    {
        $admin_user = AdminUserModel::where(['username'=>$row[1]])->first();
    
        if(empty($admin_user)){
            $data = [
                'name' => $row[0],
                'username' => $row[1],
                'mobile' => $row[2],
                'distribution_scope' => $row[3],
                'address' => $row[4],
                'latitude' => $row[5],
                'longitude' => $row[6],
                'start_time' => $row[7],
                'end_time' => $row[8],
                'openid' => 'EXCEL',
                'password' => Hash::make("abc123"),
                'type' => 2,
                'created_at'=>date('Y-m-d H:i:s',time())
            ];
            return new AdminUserModel($data);
        }
    }

    /**
     * 设置跳过表头
     */
    public function startRow(): int
    {
        return 2;
    }

    /**
     * 验证
     */
    public function rules(): array
    {
        return [
            '0' => 'max:100',
            '1' => 'max:20',
            '2' => 'max:20',
            '3' => 'max:5',
            '4' => 'max:255',
        ];
    }
    /**
     * 自定义验证信息
     */
    public function customValidationMessages()
    {
        return [
            '0.max' => '网点名称不能超过100个字',
            '1.max' => '账户手机号不能超过20个字',
            '2.max' => '电话不能超过20个字',
            '3.max' => '配送范围不能超过5个字',
            '4.max' => '网点地址不能超过255个字',
        ];

    }

}